import React, { useState, useEffect } from 'react'
import { Col, Form, Input, Button, Table } from 'antd'
import { PageHeaderWrapper } from '@ant-design/pro-layout'
import RenderButtom from '@/components/RenderButton'
import SearchBox from '@/components/SearchBox'
import { getPositionList } from '@/services/supplier/manageStaff/managePosition'

// 表头
const columns = [
  {
    title: '部门编号',
    dataIndex: 'id',
    key: 'id',
    align: 'center'
  },
  {
    title: '中文名称',
    dataIndex: 'name',
    key: 'name',
    align: 'center'
  },
  {
    title: '英文名称',
    dataIndex: 'englishName',
    key: 'englishName',
    align: 'center'
  },
  {
    title: '描述',
    dataIndex: 'depict',
    key: 'depict',
    align: 'center'
  },
  {
    title: '创建/更新人',
    dataIndex: 'createrName',
    key: 'createrName',
    align: 'center'
  },
  {
    title: '创建时间',
    dataIndex: 'createTime',
    key: 'createTime',
    align: 'center'
  },
  {
    title: '更新时间',
    dataIndex: 'updateTime',
    key: 'updateTime',
    align: 'center'
  },
  {
    title: '排序号码',
    dataIndex: 'sort',
    key: 'sort',
    align: 'center'
  },
  {
    title: '状态',
    dataIndex: 'status',
    key: 'status',
    align: 'center'
  }
]

const ManagePosition = props => {
  const { form } = props
  const { getFieldDecorator } = form
  const [positionList, setPositionList] = useState([])
  const [total, setTotal] = useState(1)
  const [pageNo, setPageNo] = useState(1)
  const [pageSize, setPageSize] = useState(10)

  useEffect(() => {
    init()
  }, [])
  const init = async () => {
    await getPositionLists()
  }
  // 表单查询
  const handleSearch = e => {
    e.preventDefault()
    form.validateFields((err, values) => {
      console.log('Received values of form: ', values)

    })
  }
  // 表单清空
  const handleReset = e => {
    form.resetFields();
  }
  // 查询条件
  const getFields = () => {
    return (
      <Col span={8}>
        <Form.Item label={`名称`}>
          {getFieldDecorator(`id`, {})
            (<Input />)}
        </Form.Item>
      </Col>
    )
  }
  // 按钮处理
  const handleBtn = type => {

  }
  // 获取部门列表
  const getPositionLists = async (pageNo = 1) => {
    let params = {
      pageNo,
      pageSize
    }
    let res = await getPositionList(params)
    if (res.code === '0') {
      setPositionList(res.data)
      setTotal(res.iTotalDisplayRecords)
    } else {
      alert(res.msg)
    }
  }

  const pagination = {
    total: parseInt(total),
    onChange(page) {
      getPositionLists(page)
    }
  }
  return (
    <PageHeaderWrapper>
      <SearchBox handleReset={handleReset} handleSearch={handleSearch} getFields={getFields} />
      <RenderButtom click={handleBtn} />
      <Table
        columns={columns}
        dataSource={positionList}
        rowKey={record => record.id}
        pagination={pagination}
      />
   </PageHeaderWrapper>
  )
}

export default Form.create()(ManagePosition)
